package com.evo.chat.common.handler;


import com.evo.chat.common.Result;
import com.evo.chat.common.exception.ChatException;
import com.evo.chat.common.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionHandler {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandler.class);

    @org.springframework.web.bind.annotation.ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handler(Exception e) {
        if (e instanceof ChatException) {
            ChatException girlException = (ChatException) e;
            return ResultUtil.error(girlException.getCode(), girlException.getMessage());
        } else {
            logger.error(" 【系统错误】{}", e);
            return ResultUtil.error(-1, "未知错误");
        }
    }

}
